package dobest.ray.com.jiongjiong.views.mesure;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import dobest.ray.com.jiongjiong.R;

/**
 * Created by wangl01 on 2016/1/27.
 */
public class MesureView extends View {
    private Context context;

    private float radus = 100;//开始半径为100px
    private float endRadus = 100;//结束半径
    private int x;//X的距离
    private int y;//Y的距离
    private Paint p;

    private static final int NONE = 0;
    private static final int DRAG = 1;
    private static final int ZOOM = 2;
    private int mode = NONE;
    private float beforeScale = 1;
    private float temScale = 1;
    private float Scale = 1;

    // 第一个按下的手指的点
    private PointF startPoint = new PointF();
    // 两个按下的手指的触摸点的中点
    private PointF midPoint = new PointF();
    // 初始的两个手指按下的触摸点的距离
    private float oriDis = 1f;
    private float tb;

    public MesureView(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public MesureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public MesureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {

        p = new Paint();
        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(4);
        p.setColor(context.getResources().getColor(R.color.grey));
        tb = context.getResources().getDimension(R.dimen.historyscore_tb);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (isFirst) {
            isFirst = false;
            radus = (int) (radus * beforeScale);
        }
        PathEffect effects = new DashPathEffect(new float[]{tb * 0.3f,
                tb * 0.3f, tb * 0.3f, tb * 0.3f}, tb * 0.1f);
        p.setPathEffect(effects);
        endRadus = radus * temScale;
        canvas.drawCircle(x / 2, y / 2, endRadus, p);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 父容器传过来的宽度方向上的模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        // 父容器传过来的高度方向上的模式
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        // 父容器传过来的宽度的值
        x = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft()
                - getPaddingRight();

        // 父容器传过来的高度的值
        y = MeasureSpec.getSize(heightMeasureSpec) - getPaddingLeft()
                - getPaddingRight();

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private boolean isFirst = false;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 进行与操作是为了判断多点触摸
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                // 第一个手指按下事件
                startPoint.set(event.getX(), event.getY());
                mode = DRAG;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                // 第二个手指按下事件
                oriDis = (float) distance(event);

                if (oriDis > 10f) {
                    midPoint = middle(event);
                    mode = ZOOM;
                    isFirst = true;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                // 手指放开事件
                //保存之前的
                Scale = beforeScale * temScale;
                beforeScale = temScale;
                isFirst = false;
                mode = NONE;
                break;
            case MotionEvent.ACTION_MOVE:
                // 手指滑动事件
                if (mode == DRAG) {
                    // 是一个手指拖动
                } else if (mode == ZOOM) {
                    // 两个手指滑动
                    double newDist = distance(event);
                    if (newDist > 10f) {
                        temScale = (float) (newDist / oriDis);
                        if (temScale != 1 && temScale > 0.2 && temScale < 3) {
                            invalidate();
                        }
                    }
                }
                break;
        }

        return true;
    }

    // 计算两个触摸点之间的距离
    private double distance(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return Math.sqrt(x * x + y * y);
    }

    // 计算两个触摸点的中点
    private PointF middle(MotionEvent event) {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        return new PointF(x / 2, y / 2);
    }

    /**
     * 获取缩放的大小
     *
     * @return
     */
    public float getMeasureScale() {
        return endRadus / 100.0f;
    }

    public float getRadus() {
        return endRadus;
    }
}
